package com.sprouting.personal.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sprouting.personal.dao.DictDao;
import com.sprouting.personal.dao.DictGroupDao;
import com.sprouting.personal.model.entity.DictGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典组
 *
 * @author ：LX（长沙麓谷）
 * @date 创建时间： 2022/7/29 8:44
 */
@Service
@Slf4j
public class DictGroupService extends ServiceImpl<DictGroupDao, DictGroup> {

    @Resource
    private DictGroupDao dictGroupDao;
    @Resource
    private DictDao dictDao;

    /**
     * 根据主键和字典组代码查询字典组
     * @param id 主键,如果传，则会过滤该ID
     * @param code 字典组代码
     * @param userId 用户ID
     * @return
     */
    public DictGroup getDictGroupByIdAndCode(String id, String code, String userId) {
        return dictGroupDao.getDictGroupByIdAndCode(id, code, userId);
    }

    /**
     * 根据主键查询字典组
     * @param id 主键
     * @return
     */
    public DictGroup getDictGroupById(String id) {
        return dictGroupDao.getDictGroupById(id);
    }

    /**
     * 分页查询字典组
     * @param page 分页参数
     * @param dictGroup 字典组参数
     * @return
     */
    public Page<DictGroup> getDictGroupListPage(Page page, DictGroup dictGroup) {
        return dictGroupDao.getDictGroupListPage(page, dictGroup);
    }

    /**
     * 删除字典组和字典
     * @param id 字典组ID
     * @param dictIdList 字典ID
     */
    @Transactional(rollbackFor = Exception.class)
    public void delDictGroupById(String id, List<String> dictIdList) {
        dictGroupDao.deleteById(id);
        if (CollUtil.isNotEmpty(dictIdList)){
            dictDao.deleteBatchIds(dictIdList);
        }
    }
}
